//页面加载事件
window.onload = function() {
	//个人信息盒子隐藏
	$('.xiugai_gerenxinxi').hide();
	$('.xiugai_youxiang').hide();
	$('.xiugai_shoujihao').hide();
	$('.xiugai_mima').hide()
/*我的课程页面*/
// 从订单表查询数据显示到页面上
//ajax请求	
	const url = '/shopping/orderfind'
	$.get(url, data => {
		console.log(data)
		let html = data.map(value => {
			const {
				orders_id: id,
				course_title: title,
				commodity_Price: price,
				course_img: img
			} = value
			return `
				<div class='center'>
					
					<div>
						<img src="${img}" alt="">
					</div>
					<div>
						${title}
					</div>
					<div>
						永久有效
					</div>
					<div>
						<button type="button">去学习</button>
					</div>
					<div>${id}</div>
				</div>
			`

		})
		$('#myCourse').append(html)
		//利用委托事件,点击按钮
		$('#myCourse').on('click', '.center>div>button', function() {
			let id = $(this).parent().siblings('.center>div:nth-child(5)').text()
			console.log(id)
			window.location.href = `course_detail.html?lid=${id}`
		})
	})



/*我的账户页面和个人资料页面*/
// sessionStorage读取当前登录的用户信息
	let id = sessionStorage.getItem('stu_id')
	console.log(id)
	//ajax请求账户余额
	const url1=`/shopping/price?stu_id=${id}`
	$.get(url1,data=>{
		console.log(data)
			const {
				stu_id:id,//id
				stu_name: name,//用户名
				stu_age:age,//年龄
				stu_mail:mail,//邮箱
				stu_phone:phone,//手机号
				stu_gender:gender,//性别
				stu_balance:balance,//账户余额
				stu_sign:sign,//个性签名	
			} = data[0]
		$('#myCount>div:nth-child(2)>span:nth-child(1)').text(balance)
		$('#myInformation>.one>div>p:nth-child(1)').text(name)
		$('#myInformation>.one>div>p:nth-child(3)').text(sign)
		$('.jibenxinxi_body>ul>li:nth-child(1)>label').text(id)
		$('.jibenxinxi_body>ul>li:nth-child(2)>label').text(name)
		$('.jibenxinxi_body>ul>li:nth-child(3)>label').text(age)
		$('.jibenxinxi_body>ul>li:nth-child(4)>label').text(gender)
		$('.jibenxinxi_body>ul>li:nth-child(5)>label').text(sign)
		$('.zhanghuanquan>ul>li:nth-child(2)>label').text(mail)
		$('.zhanghuanquan>ul>li:nth-child(3)>label').text(phone)		
	})
}
//修改用户信息输入框信息验证
//用户名验证
$('.user').focus(function(){
	this.classList.add("y");
	this.classList.remove('err');
})
$('.user').blur(function(){
	if(this.value.length>=3 &&this.value.length<=12){
		this.classList.add('y')
	}else{
		this.classList.add('err');
	}
	
})
//年龄验证
$('.age').focus(function(){
	this.classList.add("y");
	this.classList.remove('err');
})
$('.age').blur(function(){
	if(this.value>0 &&this.value<=150){
		this.classList.add('y')
	}else{
		this.classList.add('err');
	}
	
})
//个性签名验证
$('.geqian').focus(function(){
	this.classList.add("y");
})
//点击修改消息框弹出
$('.jibenxinxi_title>div:nth-child(1)>span:nth-child(2)>button').click(function(){
	$('.xiugai_gerenxinxi').show();
})
$('.xiugai_gerenxinxi>p>span>img').click(function(){
	$('.xiugai_gerenxinxi').hide();
})
//修改用户信息请求

//修改个人信息点击事件
$('.queren').click(function(){
	//获取页面元素的值
	// sessionStorage读取当前登录的用户信息
	let stu_id = sessionStorage.getItem('stu_id')
	let user =$('.user').val()
	let age= $('.age').val()
	let sex= $('.sex').val()
	let geqian=$('.geqian').val();
	$.ajax({
			type:'post',		
			url:'/user/regUpdata',
			dataType:'json',
			data:{ 
				 'user': user ,
				 'age':age,
				 'sex':sex,
				 'geqian':geqian,
				 'stu_id':stu_id
				 },
			success: function (result) {  
				console.log(result);
				if(result.code==401){
					alert('用户名不能为空')
				}
				if(result.code==402){
					alert('年龄不能小于0或大于150')
				}
				if(result.code==200){
					alert('修改成功')
					window.location.href='PersonalCenter.html'
				}
	      	}
	}) 
})

//点击修改邮箱框弹出
$('.zhanghuanquan>ul>li:nth-child(2)>button').click(function(){
	$('.xiugai_youxiang').show();
})
$('.xiugai_youxiang>div>span>img').click(function(){
	$('.xiugai_youxiang').hide();
})
//点击修改手机框弹出
$('.zhanghuanquan>ul>li:nth-child(3)>button').click(function(){
	$('.xiugai_shoujihao').show();
})
$('.xiugai_shoujihao>div>span>img').click(function(){
	$('.xiugai_shoujihao').hide();
})
//点击修改密码框弹出
$('.zhanghuanquan>ul>li:nth-child(1)>button').click(function(){
	$('.xiugai_mima').show();
})
$('.xiugai_mima>div>span>img').click(function(){
	$('.xiugai_mima').hide();
})

//修改密码点击事件
$('.mima_xiugai').click(function(){
	//获取输入框的值
	// sessionStorage读取当前登录的用户信息
	let stu_id = sessionStorage.getItem('stu_id')
	let mima_j=$('.mima_j').val()
	let mima_x=$('.mima_x').val()
	console.log(mima_j)
	console.log(mima_x)
	// ajax请求
	$.ajax({
			type:'post',		
			url:'/user/pwdUpdate',
			dataType:'json',
			data:{ 
				 'pwd_j': mima_j ,
				 'pwd_x':mima_x,
				 'stu_id':stu_id
				 },
			success: function (result) {  
				console.log(result);
				if(result.code==200){
					alert(result.msg)
					window.location.href='PersonalCenter.html'
				}
				if(result.code==402){
					alert(result.msg)
					
				}
	      	}
	}) 
})
//旧密码验证
$('.mima_j').blur(function(){
	// sessionStorage读取当前登录的用户信息
	let stu_id = sessionStorage.getItem('stu_id')
	let mima_j=$('.mima_j').val()
	console.log(mima_j)
	$.ajax({
			type:'get',		
			url:'/user/pwdYanzheng',
			dataType:'json',
			data:{ 
				 'pwd_j': mima_j ,
				 'stu_id':stu_id
				 },
			success: function (result) {  
				console.log(result);
				if(result.code==403){
					alert(result.msg)
				}
	      	}
	}) 
})
//修改手机号点击事件
$('.youxiang_xiugai').click(function(){
	//获取输入框的值
	// sessionStorage读取当前登录的用户信息
	let stu_id = sessionStorage.getItem('stu_id')
	let youxiang=$('.youxiang').val()
	console.log(youxiang)
	// ajax请求
	$.ajax({
			type:'post',		
			url:'/user/emailUpdate',
			dataType:'json',
			data:{ 
				 'youxiang': youxiang ,
				 'stu_id':stu_id
				 },
			success: function (result) {  
				console.log(result);
				if(result.code==200){
					alert(result.msg)
					window.location.href='PersonalCenter.html'
				}
				if(result.code==402){
					alert(result.msg)
					
				}
	      	}
	}) 
})
//修改邮箱点击事件
$('.shoujihao_xiugai').click(function(){
	//获取输入框的值
	// sessionStorage读取当前登录的用户信息
	let stu_id = sessionStorage.getItem('stu_id')
	let shoujihao=$('.shoujihao').val()
	console.log(shoujihao)
	// ajax请求
	$.ajax({
			type:'post',		
			url:'/user/phonelUpdate',
			dataType:'json',
			data:{ 
				 'shoujihao': shoujihao ,
				 'stu_id':stu_id
				 },
			success: function (result) {  
				console.log(result);
				if(result.code==200){
					alert(result.msg)
					window.location.href='PersonalCenter.html'
				}
				if(result.code==402){
					alert(result.msg)
					
				}
	      	}
	}) 
})





